#include "kurcaci.h"

#include <iostream>
using namespace std;

int Kurcaci::getObjectType() const {
	return tipe_object;
}

Kurcaci::Kurcaci(): Object(2){
	status_sleep = true;
	faceto = -1;
	searchdir = -1;
}

Kurcaci::~Kurcaci() {
}

void Kurcaci::SetX(int newX) {
	location.mid.x = newX;
}

void Kurcaci::SetY(int newY) {
	location.mid.y = newY;
}

void Kurcaci::Wake() {
	status_sleep = false;
}

void Kurcaci::Sleep() {
	status_sleep = true;
	searchdir = -1;
}

int Kurcaci::GetX() const {
	return location.mid.x;
}

int Kurcaci::GetY() const {
	return location.mid.y;
}

int Kurcaci::GetStatusSleep() const {
	return status_sleep;
}

bool Kurcaci::isSleep () const {
	return status_sleep;
}

point Kurcaci::getpos() const{
	return location.mid;
}

point Kurcaci::getvektor() const{
	return vektor;
}

int Kurcaci::getFace() const{
	return face;
}	

void Kurcaci::setpos(int x, int y) {   
    location.mid.x   = x;   location.mid.y   = y;
    location.left.x  = x-1; location.left.y  = y;
    location.right.x = x+1; location.right.y = y;
    location.up.x    = x;   location.up.y    = y-1;
    location.down.x  = x;   location.down.y  = y+1;
}

void Kurcaci::setvektor(point value) {
	vektor.x = value.x;vektor.y = value.y;
}	

void Kurcaci::setFace (int dir) {
    if      (dir == 0)   {vektor = location.left; face = 0;}
    else if (dir == 1)   {vektor = location.right; face = 1;}
    else if (dir == 2)   {vektor = location.up; face = 2;}
    else if (dir == 3)   {vektor = location.down; face = 3;}
}
	
void Kurcaci::moveLeft(field* afield, int xplayer, int yplayer,int xkurcwater,int ykurcwater,int xkurcharvest,int ykurcharvest,int xkurcslash,int ykurcslash) {
	if (face == 0){
		if(((vektor.x!=xplayer || vektor.y!=yplayer) && (vektor.x!=xkurcharvest || vektor.y != ykurcharvest) && (vektor.x!=xkurcslash || vektor.y!=ykurcslash) && (vektor.x!=xkurcwater || vektor.y != ykurcwater)) && afield->IsWalkable(vektor.x,vektor.y)) {
			point current = getpos();
			setpos(current.x - 1, current.y);
			setvektor(location.left);
		}else{//kalo ada hambatan di depan kurcaci
			faceto = 0;
			if(afield->IsWalkable(location.up.x,location.up.y)){
				searchdir = 2;
			}else if(afield->IsWalkable(location.down.x,location.down.y)) {
				searchdir = 3;
			}else if(afield->IsWalkable(location.right.x,location.right.y)) {
				searchdir = 1;
			}
		}
	}else{
		turn_left();
	}	
}	

void Kurcaci::moveRight(field* afield,int xplayer, int yplayer,int xkurcwater,int ykurcwater,int xkurcharvest,int ykurcharvest,int xkurcslash,int ykurcslash) {
    if (face == 1) {
		if(((vektor.x!=xplayer || vektor.y!=yplayer) && (vektor.x!=xkurcharvest || vektor.y != ykurcharvest) && (vektor.x!=xkurcslash || vektor.y!=ykurcslash) && (vektor.x!=xkurcwater || vektor.y != ykurcwater)) && afield->IsWalkable(vektor.x,vektor.y)) {
			point current = getpos();
			setpos(current.x + 1, current.y);
			setvektor(location.right);
		}else{//kalo ada hambatan depan kurcaci
			faceto = 1;
			if(afield->IsWalkable(location.up.x,location.up.y)){
				searchdir = 2;
			}else if(afield->IsWalkable(location.down.x,location.down.y)) {
				searchdir = 3;
			}else if(afield->IsWalkable(location.left.x,location.left.y)) {
				searchdir = 0;
			}
		}
	} else {
		turn_right();
	}
}

void Kurcaci::moveUp(field* afield,int xplayer, int yplayer,int xkurcwater,int ykurcwater,int xkurcharvest,int ykurcharvest,int xkurcslash,int ykurcslash) {
    if (face == 2) {
		if(((vektor.x!=xplayer || vektor.y!=yplayer) && (vektor.x!=xkurcharvest || vektor.y != ykurcharvest) && (vektor.x!=xkurcslash || vektor.y!=ykurcslash) && (vektor.x!=xkurcwater || vektor.y != ykurcwater)) && afield->IsWalkable(vektor.x,vektor.y)) {
			point current = getpos();
			setpos(current.x, current.y - 1);
			setvektor(location.up);
		}else{//kalo ada hambatan depan kurcaci
			faceto = 2;
			if(afield->IsWalkable(location.left.x,location.left.y)){
				searchdir = 0;
			}else if(afield->IsWalkable(location.down.x,location.down.y)) {
				searchdir = 3;
			}else if(afield->IsWalkable(location.right.x,location.right.y)) {
				searchdir = 1;
			}
		}
	} else {
		turn_up();
	}
}

void Kurcaci::moveDown(field* afield,int xplayer, int yplayer,int xkurcwater,int ykurcwater,int xkurcharvest,int ykurcharvest,int xkurcslash,int ykurcslash) {
    if (face == 3) {
		if (((vektor.x!=xplayer || vektor.y!=yplayer) && (vektor.x!=xkurcharvest || vektor.y != ykurcharvest) && (vektor.x!=xkurcslash || vektor.y!=ykurcslash) && (vektor.x!=xkurcwater || vektor.y != ykurcwater)) && afield->IsWalkable(vektor.x,vektor.y)) {
			point current = getpos();
			setpos(current.x, current.y + 1);
			setvektor(location.down);
		}else{//kalo ada hambatan depan kurcaci
			faceto = 3;
			if(afield->IsWalkable(location.left.x,location.left.y)){
				searchdir = 0;
			}else if(afield->IsWalkable(location.up.x,location.up.y)) {
				searchdir = 2;
			}else if(afield->IsWalkable(location.right.x,location.right.y)) {
				searchdir = 1;
			}
		}
	}else{
		turn_down();
	}
}

void Kurcaci::turn_left() {
    vektor = location.left; face = 0; 
}

void Kurcaci::turn_right() {
	vektor = location.right; face = 1;
}

void Kurcaci::turn_up() {
	vektor = location.up; face = 2;
}
    
void Kurcaci::turn_down() {
	vektor = location.down; face = 3; 
}

